/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.LookupOp;
import java.awt.image.LookupTable;
import java.io.IOException;
import net.worlds.AssetManager;
import net.worlds.WorldsException;
import net.worlds.Main;
import net.worlds.PropChangeHandler;
import org.jbox2d.common.Vec2D;


public class Ladder extends Box{
    public static BufferedImage ladder;
    private ColorLookupTable tbl = new ColorLookupTable(0, 4);
    private Color mainColor;
    private Color borderColor;
    public Ladder() throws WorldsException
    {
        super("Ladder");
        addPropChangeHandler("Height", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                setProp("Size", new Vec2D(4, (Float) value), true);
                rebuildTexture();
            }
        });
        addPropChangeHandler("Color", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                mainColor = ((Color)value);
                borderColor = ((Color)value).darker();
                if(inWorld)
                    rebuildTexture();
            }
        });
    }
    private void rebuildTexture() throws WorldsException
    {
        if(Main.noView)
            return;
        Vec2D size = getVec2DProp("Size").mul(8);
        LookupOp op = new LookupOp(tbl, null);
        setTexture(op.filter(AssetManager.tileTexture(ladder, (int) size.x, (int) size.y, false, true, false, false), null));
    }
    @Override
    public void onPersisted(Part other)
    {
        try {
            if (other.getParent() instanceof Character && ((Character)other.getParent()).isPlayer()) {
                if(Main.getLocalPlayer() != null)
                    Main.getLocalPlayer().setLadder(this);
            }
        } catch (WorldsException ex) {
            ex.printStackTrace();
        }
    }
    @Override
    public void onUncollided(Part other)
    {
        try {
            if (other.getParent() instanceof Character && ((Character)other.getParent()).isPlayer()) {
                Main.getLocalPlayer().setLadder(null);
            }
        } catch (WorldsException ex) {
            ex.printStackTrace();
        }
    }
    public void addToWorld() throws WorldsException
    {
        super.addToWorld();
        rebuildTexture();
    }
    private class ColorLookupTable extends LookupTable
    {
        public ColorLookupTable(int offset, int numComponents)
        {
            super(offset, numComponents);
        }
        @Override
        public int[] lookupPixel(int[] src, int[] dest) {
            int[] r = null;
            //System.err.println(src[0]);
            if(src[0] == 166)
            {
                r = new int[]{mainColor.getRed(), mainColor.getGreen(), mainColor.getBlue(), 255};
            }
            else if(src[0] == 124)
            {
                r = new int[]{borderColor.getRed(), borderColor.getGreen(), borderColor.getBlue(), 255};
            }
            if(r == null)
            {
                if(dest != null)
                {
                    dest[0] = src[0];
                    dest[1] = src[1];
                    dest[2] = src[2];
                    dest[3] = src[3];
                }
                return src;
            }
            else
            {
                if(dest != null)
                {
                    dest[0] = r[0];
                    dest[1] = r[1];
                    dest[2] = r[2];
                    dest[3] = r[3];
                }
                return r;
            }
        }
    }

    static {
        try {
            if(!Main.noView)
                ladder = AssetManager.loadImage("ladder", "resource://ladder.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
